package bud.web.controller;

import java.util.List;

import javax.inject.Inject;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import bud.vo.BoardFormDTO;
import bud.vo.ReplyDTO;
import bud.vo.ReplyshopDTO;
import bud.web.boardbud.service.IBoardbudService;
import bud.web.boardshop.service.IBoardshopService;

@Controller
@Slf4j
public class ReplyshopController {
	@Inject
	IBoardshopService service;
	
	@RequestMapping("replyshopInsert")
	public String replyshopInsert(ReplyshopDTO replyshop, Model model) {

		int result = service.insertReplyshop(replyshop);
		BoardFormDTO<ReplyshopDTO> form = new BoardFormDTO<ReplyshopDTO>();
		String jsonResult = "";
		if (result > 0) {
			form.setState("success");
			form.setBsh_no(replyshop.getRsh_ref_key());
			form.setCurrentPage(1);
			form.setScreen_size(10);
			form.setBlock_size(5);
			form.setTotalRecords(service.getRshCount(replyshop.getRsh_ref_key()));
			List<ReplyshopDTO> rshList = service.getRshList(form);
			form.setDataList(rshList);
		} else {
			form.setState("failed");
			form.setMessage("데이터베이스 오류, 잠시 후 다시 실행해보세요");
		}
		model.addAttribute("form",form);
		return "jsonView";
	}
	
	@RequestMapping("replyshopList")
	public String replyshopList(@ModelAttribute("form")BoardFormDTO<ReplyshopDTO> form) {
		form.setScreen_size(10);
		form.setBlock_size(5);
		form.setTotalRecords(service.getRshCount(form.getBsh_no()));
		form.setState("success");
		List<ReplyshopDTO> rshList = service.getRshList(form);
		form.setDataList(rshList);
		
		return "jsonView";
	}
	
	@RequestMapping("replyshopDelete")
	public String replyshopDelete(ReplyshopDTO replyshop, Model model) {
		int result = service.deleteReplyshop(replyshop);
		BoardFormDTO<ReplyshopDTO> form = new BoardFormDTO<ReplyshopDTO>();
		if(result > 0) {
			form.setState("success");
			form.setBsh_no(replyshop.getRsh_ref_key());
			form.setCurrentPage(1);
			form.setScreen_size(10);
			form.setBlock_size(5);
			form.setTotalRecords(service.getRshCount(replyshop.getRsh_ref_key()));
			List<ReplyshopDTO> rshList = service.getRshList(form);
			form.setDataList(rshList);
		} else {
			form.setState("failed");
			form.setMessage("비밀번호 오류");
		}
		model.addAttribute("form",form);
		return "jsonView";
	}
}
